package com.twelve.all.utils;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.twelve.all.R;


/**
 * Created by 张龙臣 on 2019/1/12.
 */

public abstract class ConformDialog extends AlertDialog {


    private TextView mTvConfirm;
    private TextView mTvCancel;
    private TextView mTvMsg;

    protected ConformDialog(@NonNull Context context) {
        super(context);
        init(context, null, null);
    }

    protected ConformDialog(@NonNull Context context, String msg) {
        super(context);
        init(context, null, msg);
    }

    protected ConformDialog(@NonNull Context context, String title, String msg) {
        super(context);
        init(context, title, msg);
    }

    private void init(Context context, String title, String msg) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.dialog_confirm, null);
        LinearLayout ll = (LinearLayout) inflate.findViewById(R.id.ll);
        mTvConfirm = (TextView) inflate.findViewById(R.id.tvConfirm);
        mTvCancel = (TextView) inflate.findViewById(R.id.tvCancel);
        TextView tvTitle = (TextView) inflate.findViewById(R.id.tvTitle);
        mTvMsg = (TextView) inflate.findViewById(R.id.tvMsg);

        if (TextUtils.isEmpty(title))
            tvTitle.setText("提示");
        else tvTitle.setText(title);
        if (!TextUtils.isEmpty(msg))
            mTvMsg.setText(msg);
        setView(inflate);
        setCancelable(false);
        mTvConfirm.setOnClickListener(new NotFastClickListener() {
            @Override
            public void isNotFastClick(View v) {
                onConfirm();
                dismiss();
            }
        });
        mTvCancel.setOnClickListener(new NotFastClickListener() {
            @Override
            public void isNotFastClick(View v) {
                onCancel();
                dismiss();
            }
        });
        if (!isShowing())
            show();
    }

    public void setConfirmText(String text) {
        mTvCancel.setText(text);
    }

    public void setCancelText(String text) {
        mTvConfirm.setText(text);
    }

    protected void onCancel() {
    }

    protected abstract void onConfirm();

    public void setMsgShow(String msg) {
        if (mTvMsg != null && msg != null)
            mTvMsg.setText(msg);
        show();
    }

}
